package cdu.bbs.bbsv2.service.impl;

import cdu.bbs.bbsv2.domain.User;
import cdu.bbs.bbsv2.mapper.TopicMapper;
import cdu.bbs.bbsv2.domain.Topic;
import cdu.bbs.bbsv2.service.AdminService;
import cdu.bbs.bbsv2.service.TopicService;
import cdu.bbs.bbsv2.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    public UserService userService;
    @Autowired
    public AdminService adminService;
    @Autowired
    public TopicMapper topicMapper ;

    @Override
    public List<Topic> getTopics() {
        return topicMapper.getTopics();
    }

    // 分页获取未置顶的帖子 v2
    @Override
    public PageInfo<Topic> getNotTop(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> notTop = topicMapper.getNotTop();
        return new PageInfo<>(notTop);
    }


    @Override
    public PageInfo<Topic> getTopics(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> topics = topicMapper.getTopics();
        List<Topic> t = new ArrayList<>();

        for(Topic topic :topics){
            if ("00".equals(topic.getStatus())){
                t.add(topic);
            }
        }

        return new PageInfo<>(t);
    }

    @Override
    public PageInfo<Topic> getTopicsByUserId(int pageNum, int pageSize,String userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> topics = topicMapper.getTopicsByUserId(userId);

        return new PageInfo<>(topics);
    }

    @Override
    public PageInfo<Topic> getCollectionTopicsByUserId(int pageNum, int pageSize, String userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> topics = topicMapper.getCollectionTopicsByUserId(userId);

        return new PageInfo<>(topics);
    }

    @Override
    public List<Topic> getTopicsByUserId(String userId) {
        List<Topic> topics = topicMapper.getTopicsByUserId(userId);
        return topics;
    }



    @Override
    public Boolean deleteTopicById(String id) {
        int row = topicMapper.deleteTopicById(id);
        return row == 1;
    }

    @Override
    public Boolean addTopic(Topic topic) {
        int row = topicMapper.addTopic(topic);
        return row == 1;
    }



    @Override
    public List<Topic> getTopicsByCondition(Map<String, String> conditions) {
        return topicMapper.getTopicsByCondition(conditions);
    }

    @Override
    public Topic getTopicById(String id) {
        return topicMapper.getTopicById(id);
    }

    @Override
    public boolean updateInfo(Topic topic) {
        return topicMapper.updateTopicInfo(topic) == 1;
    }

    @Override
    public int getUserThumbCount(List<Topic> topics) {
        int thumbCount = 0;
        for (Topic topic : topics) {
            thumbCount += topic.getThumbsCount();
        }
        return thumbCount;
    }

    @Override
    public int geUserCollectionCount(List<Topic> topics) {
        int collectionCount = 0;
        for (Topic topic : topics) {
            collectionCount += topic.getCollectionCount();
        }
        return collectionCount;
    }

    @Override
    public int getUserTopicCount(List<Topic> topics) {
        return topics.size();
    }


    @Override
    public Map<Topic, User> getTopTopicAndUser() {
        List<Topic> topics = topicMapper.getTopicsTop();
        Map<Topic,User> map = new HashMap<>();
        for (Topic topic : topics) {
            User user = userService.getUserById(topic.getCreateBy());
            map.put(topic,user);
        }
        return map;
    }


    @Override
    public Boolean top(String id) {
        Topic topic = topicMapper.getTopicById(id);
        StringBuilder status = new StringBuilder(topic.getStatus());
        status.setCharAt(0,'1');
        topic.setStatus(new String(status));
        return updateInfo(topic);
    }

    @Override
    public Boolean essence(String id) {
        Topic topic = topicMapper.getTopicById(id);
        StringBuilder status = new StringBuilder(topic.getStatus());
        status.setCharAt(1,'1');
        topic.setStatus(new String(status));
        return updateInfo(topic);
    }

    @Override
    public Boolean deleteTopicByIds(String[] ids) {
        int row = topicMapper.deleteTopicByIds(ids);
        System.out.println(row);
        return row == ids.length;
    }

    @Override
    public Boolean topTopicByIds(String[] ids) {
        int row = topicMapper.topTopicByIds(ids);
        return row == ids.length;
    }
    @Override
    public boolean insertTopicCollection(String topicId, String userId) {
        return topicMapper.insertTopicCollection(topicId,userId) == 1;
    }

    @Override
    public Boolean removeTopicCollection(String topicId, String userId) {
        return topicMapper.removeTopicCollection(topicId,userId) == 1;
    }

    @Override
    public boolean insertTopicThumb(String topicId, String userId) {
        return topicMapper.insertTopicThumb(topicId,userId) == 1;

    }

    @Override
    public Boolean removeTopicThumb(String topicId, String userId) {
        return topicMapper.removeTopicThumb(topicId,userId) == 1;
    }

    @Override
    public PageInfo<Topic> getThumbTopicsByUserId(int pageNum, int pageSize, String id) {
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> topics = topicMapper.getThumbTopicsByUserId(id);

        return new PageInfo<>(topics);
    }

}
